package co.edu.udistrital.erpgrid.bd.modelo.connection;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;

/**
 * This merges all JPA entities from multiple jars. To use it, all entities must
 * be listed in their respective persistence.xml files using the <class> tag.
 * 
 * @see http://forum.springsource.org/showthread.php?t=61763
 * 
 */
public class MergingPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {
	
	/**
	 * Maneja las clases obtenidas por unidad de persistencia.
	 * @uml.property  name="puiClasses"
	 * @uml.associationEnd  qualifier="getPersistenceUnitName:java.lang.String java.util.Set"
	 */
	private final transient Map<String, Set<String>> puiClasses = new HashMap<String, Set<String>>();

	/* (non-Javadoc)
	 * @see org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor#postProcessPersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo)
	 */
	
	public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
		Set<String> classes = puiClasses.get(pui.getPersistenceUnitName());
		if (classes == null) {
			classes = new HashSet<String>();
			puiClasses.put(pui.getPersistenceUnitName(), classes);
		}
		pui.getManagedClassNames().addAll(classes);
		classes.addAll(pui.getManagedClassNames());
	}
}